package com.ztesoft.huangfu.learning.springboot.springbootrestful.controller;

import com.ztesoft.huangfu.learning.springboot.springbootrestful.entity.Student;
import com.ztesoft.huangfu.learning.springboot.springbootrestful.service.ProcessResult;
import com.ztesoft.huangfu.learning.springboot.springbootrestful.service.Staff;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by codeya on 2017/12/5.
 */
@RestController
@Slf4j
public class StaffController {

    List list = new ArrayList();
    public StaffController() {
        for (int i = 0; i < 10000; i++) {
            Student student = new Student(i,"huangfu",i);
            list.add(student);
        }
    }

    @PostMapping(value="/hello/{id}")
    public ProcessResult test_path_variable(@PathVariable("id")String id) {
        log.info(id);

        ProcessResult result = new  ProcessResult(ProcessResult.SUCCESS,"OK","list="+list);
        return result;
    }

    @RequestMapping(value="/test_path_and_param/{id}")
    public ProcessResult test_path_and_param(@PathVariable("id")String id,@RequestParam("name")String name ) {
        log.info(id.toString());
        ProcessResult result = new  ProcessResult(ProcessResult.SUCCESS,"OK","id="+id+",name="+name);
        return result;
    }
    @RequestMapping(value="/test_request_param")
    public ProcessResult test_request_param(@RequestParam ("id")String id,@RequestParam("name")String name) {
        log.info("id:{},name:{}",id.toString(),name.toString());
        ProcessResult result = new  ProcessResult(ProcessResult.SUCCESS,"OK","id="+id+"name="+name);
        return result;
    }
    /**
     * post方式传递自定义对象。。ajax调用时需要json写法
     * @return
     */
    @PostMapping("test_questbody")
    public ProcessResult test_questbody(@RequestBody Staff staff) {
        log.info(staff.toString());
        //staff.setBirthday(null);
        ProcessResult result = new  ProcessResult(ProcessResult.SUCCESS,"OK",staff);
        return result;
    }
    @PostMapping("test_path_body/{id}")
    public ProcessResult test_path_body(@PathVariable("id") String id,@RequestBody Staff staff) {
        log.info("id"+id+",staff:"+staff.toString());
        //staff.setBirthday(null);
        ProcessResult result = new  ProcessResult(ProcessResult.SUCCESS,"OK",staff);
        return result;
    }

}
